package com.atguigu.hotitems_analysis.analysis;

import com.atguigu.hotitems_analysis.beans.ItemViewCount;
import com.atguigu.hotitems_analysis.beans.UserBehavior;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.sql.Timestamp;
import java.util.*;

/**
 * @author zkq
 * @date 2022/10/2 10:33
 */
public class HotItems {
    public static void main(String[] args) throws Exception{
        //1.创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.读取数据，创建DataStream数据流
        DataStreamSource<String> inputStream = env.readTextFile("F:\\javasecode220620\\UserBehaviorAnalysis\\HotItemsAnalysis\\src\\main\\resources\\UserBehavior.csv");

        //3.转换为POJO，分配时间戳和watermark
        SingleOutputStreamOperator<UserBehavior> Stream = inputStream
                .map(data -> {
                    String[] splits = data.split(",");
                    return new UserBehavior(new Long(splits[0]), new Long(splits[1]), new Integer(splits[2]),
                            splits[3], new Long(splits[4]));
                })
                .assignTimestampsAndWatermarks(WatermarkStrategy.<UserBehavior>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<UserBehavior>() {
                            @Override
                            public long extractTimestamp(UserBehavior element, long recordTimestamp) {
                                return element.getTimestamp() * 1000;
                            }
                        })
                );

        //4.分组聚合开窗 得到每个窗口内各商品的count值
        SingleOutputStreamOperator<ItemViewCount> aggregateResult = Stream
                //过滤出行为是pv的数据
                .filter(data -> "pv".equals(data.getBehavior()))
                .keyBy(data -> data.getItemId())
                .window(SlidingEventTimeWindows.of(Time.hours(1), Time.minutes(5)))
                .aggregate(new ItemCountAgg(), new WindowCountResult());

        //5.收集同一窗口的所有商品的count值，排序输出TopN
        SingleOutputStreamOperator<String> result = aggregateResult
                .keyBy(data -> data.getWindowEnd())
                .process(new topNHotItems(5));

        result.print();

        env.execute("hot items analysis");
    }
    public static class ItemCountAgg implements AggregateFunction<UserBehavior,Long,Long>{
        @Override
        public Long createAccumulator() {
            return 0L;
        }

        @Override
        public Long add(UserBehavior value, Long accumulator) {
            return accumulator + 1;
        }

        @Override
        public Long getResult(Long accumulator) {
            return accumulator;
        }

        @Override
        public Long merge(Long a, Long b) {
            return a + b;
        }
    }

    public static class WindowCountResult extends ProcessWindowFunction<Long,ItemViewCount, Long, TimeWindow>{
        @Override
        public void process(Long aLong, Context context, Iterable<Long> elements, Collector<ItemViewCount> out) throws Exception {
            long itemId = aLong;
            long windowEnd = context.window().getEnd();
            long count = elements.iterator().next();
            out.collect(new ItemViewCount(itemId,windowEnd,count));
        }
    }

    public static class topNHotItems extends KeyedProcessFunction<Long,ItemViewCount,String>{
        private Integer topN;

        //定义状态，所有相同key的ItemViewCount都存在状态里
        ListState<ItemViewCount> listState;

        //topN方法需要传参指定N具体是几
        public topNHotItems(Integer n) {
            topN = n;
        }

        @Override
        public void open(Configuration parameters) throws Exception {
            listState = getRuntimeContext().getListState(new ListStateDescriptor<ItemViewCount>("item-count-list", ItemViewCount.class));
        }
        //数据来了 把数据存入状态里 然后注册定时器
        @Override
        public void processElement(ItemViewCount value, Context ctx, Collector<String> out) throws Exception {
            listState.add(value);
            ctx.timerService().registerEventTimeTimer(value.getWindowEnd()+1L);
        }
        //定时器时间到了 说明上面窗口的数据都到齐了，那么就要触发排序了
        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
            ArrayList<ItemViewCount> list = new ArrayList<>();
            Iterable<ItemViewCount> itemViewCounts = listState.get();
            for (ItemViewCount itemViewCount : itemViewCounts) {
                list.add(itemViewCount);
            }
            //从大到小排序
            list.sort(new Comparator<ItemViewCount>() {
                @Override
                public int compare(ItemViewCount o1, ItemViewCount o2) {
                    return o2.getCount().intValue() - o1.getCount().intValue();
                }
            });

            //将排名信息处理返回
            //用buffer或者builder好处理 字符串接拼麻烦
            StringBuilder result = new StringBuilder();
            result.append("==================================\n");
            result.append("窗口结束时间：").append(new Timestamp(timestamp-1L)).append("\n");

            for (int i = 0; i<Math.min(list.size(),topN); i++){
                ItemViewCount itemViewCount = list.get(i);
                result.append("No").append(i+1).append(":")
                        .append(" 商品ID=")
                        .append(itemViewCount.getItemId())
                        .append(" 浏览量=")
                        .append(itemViewCount.getCount())
                        .append("\n");

            }
            result.append("====================================\n\n");
            // 控制输出频率，模拟实时滚动结果
            //第一个窗口应该是 8：05-9:05 数据是从9；00开始 但是窗口左闭右开所以9:00数据第一次进窗口应该是进到8:05-9:05的窗口
            Thread.sleep(5000);
            out.collect(result.toString());
        }
    }
}
